import React, { Component } from 'react';
import { View, Text, Alert,TouchableOpacity } from 'react-native';
import Dimensions from 'Dimensions';
import SplashScreen from 'react-native-splash-screen';
import Platform from 'Platform';

import Config from './config';
import Constants from './common/Constants';
import SizeUtils from './common/SizeUtils';
import CommonStyles from './common/CommonStyles';
import Routes from './common/Routes';
import ActionManager from './actions/ActionManager';
import DataManager from './datas/DataManager';
import CommonExecuter from './executers/common/CommonExecuter';
import WorkHardExecuter from './executers/workhard/WorkHardExecuter';
import LoginExecuter from './executers/login/LoginExecuter';
import UserExecuter from './executers/user/UserExecuter';
import ActivityExecuter from './executers/activity/ActivityExecuter';
import NewsExecuter from './executers/news/NewsExecuter';
import HeartExecuter from './executers/heart/HeartExecuter';
import SportExecuter from './executers/sport/SportExecuter';
import VipSerCardExecuter from './executers/vipsercard/VipSerCardExecuter';
import GuahaoExecuter from './executers/guahao/GuahaoExecuter';
import QuanZiExecuter from './executers/quanzi/QuanZiExecuter';
import ZahuopuExecuter from './executers/zahuopu/ZahuopuExecuter';
import PostExecuter from './executers/post/PostExecuter';
import JifenMallExecuter from './executers/jifenmall/JifenMallExecuter';
import ShuwuExecuter from './executers/shuwu/ShuwuExecuter';
import NoticeExecuter from './executers/notice/NoticeExecuter';
import JianyiExecuter from './executers/jianyi/JianyiExecuter';

import ImageUtil from './pages/common/ImageUtil';

import ToastUtils from './common/ToastUtils';
import TNavigator from './TNavigator';
import ImageFullScreenView from './pages/common/ImageFullScreenView';
import CommonOptionView from './pages/common/CommonOptionView';
import CommonPickerView from './pages/common/CommonPickerView';
import TVideoComponent from './components/TVideoComponent';
export default class Stage extends Component {
  constructor() {
    super();

    if (!__DEV__) {
      global.console = {
        info: () => {},
        log: () => {},
        warn: () => {},
        error: () => {}
      };
    } else {
      global.console.warn = () => {};
      // global.console.error = () => {};
    }

    this.state = {
      loadCount: 1,
      showImageFS: false,
      imageData: null,
      showOption: false,
      optionData: [],
      pickerData: { isShow: false },
      showFullScreenView: false,
      fullScreenViewData: {}
    };
    console.log(Dimensions.get('window'));
    SizeUtils.setWindowSize(Dimensions.get('window').width, Dimensions.get('window').height);
    console.log('windowWidth:' + SizeUtils.getWindowSize().width);
    console.log('windowHeight:' + SizeUtils.getWindowSize().height);

    let paddingTop = SizeUtils.translateDesignHeightToCode(Platform.OS === 'ios' ? 130 : 100),
      marginBottom = 0;
    let tabBarTop = Platform.OS === 'ios' ? SizeUtils.translateDesignHeightToCode(64) : 0;
    let statusBarHeight = Platform.OS === 'ios' ? SizeUtils.translateDesignHeightToCode(40) : 0;
    Config.tabBarTop = tabBarTop;
    Config.paddingTop = paddingTop;
    Config.statusBarHeight = statusBarHeight;
    Config.marginBottom = marginBottom;

    if (Platform.OS === 'ios') {
      Config.app_os = 2;
      Config.version_app = Config.version_app_ios;
      Config.app_url = Config.app_url_ios;
    } else if (Platform.OS === 'android') {
      Config.app_os = 1;
      Config.version_app = Config.version_app_android;
      Config.app_url = Config.app_url_android;
    }

    ActionManager.registeExecuter(
      Constants.MODULE_TYPE.COMMON,
      new CommonExecuter({ stage: this })
    );
    ActionManager.registeExecuter(Constants.MODULE_TYPE.WORK_HARD, new WorkHardExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.LOGIN, new LoginExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.USER, new UserExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.ACTIVITY, new ActivityExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.NEWS, new NewsExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.HEART, new HeartExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.SPORT, new SportExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.GUAHAO, new GuahaoExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.VIPSERCARD, new VipSerCardExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.QUAN_ZI, new QuanZiExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.ZAHUOPU, new ZahuopuExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.POST, new PostExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.JIFENMALL, new JifenMallExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.SHUWU, new ShuwuExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.NOTICE, new NoticeExecuter());
    ActionManager.registeExecuter(Constants.MODULE_TYPE.JIANYI, new JianyiExecuter());

    Constants.stage = this;

    DataManager.setup(this._callByData.bind(this));

    require('ErrorUtils').setGlobalHandler(function(err) {
      if (Platform.OS !== 'ios') {
        ImageUtil.uploadError('msg:' + err.message + '  =====' + err.stack);
        return !__DEV__;
      }
    });
  }

  _callByData(callbackData) {
    this.setState({
      loadCount: this.state.loadCount - 1
    });
  }

  async componentDidMount() {
    // await sleep(5000);
    await SplashScreen.hide();
  }

  render() {
    if (this.state.loadCount == 0) {
      return (
        <View style={{ height: SizeUtils.getWindowSize().height, backgroundColor: '#ffffff' }}>
          <TNavigator
            style={{ height: SizeUtils.getWindowSize().height }}
            dataManager={DataManager}
            routes={Routes}
          />

          {this._renderShowImageFS()}
          {this._renderOptionView()}
          {this._renderPickerView()}
          {this._renderFullScreenView()}
          {this._renderVideo()}
        </View>
      );
    } else {
      return (
        <View style={{ paddingTop: 200 }}>
          <Text />
        </View>
      );
    }
  }

  _renderVideo() {
    if (this.state.showVideoView) {
      return (
        <View
          style={{
            position: 'absolute',
            top: 0,
            width: SizeUtils.getWindowSize().width,
            height: SizeUtils.getWindowSize().height,
            backgroundColor:'#0006',
          }}
        >
          <TouchableOpacity activeOpacity={1} style={{flex:1}} onPress={this._onCloseVideo.bind(this)}/>
          <TVideoComponent
            style={this.state.videoData.style}
            videoUrl={this.state.videoData.videoUrl}
            canFullScreen={this.state.videoData.canFullScreen}
            showClose={this.state.videoData.showClose}
            onClose={this._onCloseVideo.bind(this)}
          />
          <TouchableOpacity activeOpacity={1} style={{flex:1}} onPress={this._onCloseVideo.bind(this)}/>
        </View>
      );
    }
  }

   _onCloseVideo() {
    this.setState({
      showVideoView: false,
      videoData: null
    });
  }

  showVideo(data) {
    // console.log(component);
    if (data === null) {
      this.setState({
        showVideoView: false
      });
    } else {
      this.setState({
        showVideoView: true,
        videoData: data
      });
    }
  }

  closeVideo() {
    if (this.state.showVideoView) {
      this.setState({
        showVideoView: false
      });
      return true;
    }
    return false;
  }
  
  _renderFullScreenView() {
    if (this.state.showFullScreenView) {
      return (
        <View
          style={{
            position: 'absolute',
            top: 0,
            width: SizeUtils.getWindowSize().width,
            height: SizeUtils.getWindowSize().height
          }}
        >
          <this.state.fullScreenViewData.component
            page={this.state.fullScreenViewData.page}
            updateMark={this.state.fullScreenViewData.updateMark}
            fullScreenData={this.state.fullScreenViewData.data}
            onClose={this._onCloseFullScreenView.bind(this)}
          />
        </View>
      );
    }
  }


  _onCloseFullScreenView() {
    this.setState({
      showFullScreenView: false,
      fullScreenViewData: null
    });
  }

  showFullScreen(data) {
    // console.log(component);
    if (data === null) {
      this.setState({
        showFullScreenView: false
      });
    } else {
      this.setState({
        showFullScreenView: true,
        fullScreenViewData: data
      });
    }
  }

  closeFullScreen() {
    if (this.state.showFullScreenView) {
      this.setState({
        showFullScreenView: false
      });
      return true;
    }
    return false;
  }

  _renderShowImageFS() {
    if (this.state.showImageFS) {
      return (
        <ImageFullScreenView
          style={{ position: 'absolute', top: 0 }}
          data={this.state.imageData}
          // return <ImageFullScreenView style={{position:"absolute", top:0}} data = {require("../resource/images/common/avatar.png")}
          onClose={this._onCloseImageFS.bind(this)}
        />
      );
    }
  }

  _onCloseImageFS() {
    this.setState({
      showImageFS: false
    });
  }

  _renderOptionView() {
    if (this.state.showOption) {
      return (
        <CommonOptionView
          style={{ position: 'absolute', top: 0 }}
          optionData={this.state.optionData}
          // let optionData = [{text:"保存图片"}, {text:"保存图片2"}];
          // return <CommonOptionView style={{position:"absolute", top:0}} optionData = {optionData}
          onClose={this._onCloseOptionView.bind(this)}
        />
      );
    }
  }

  _onCloseOptionView(selectedValue) {
    this.setState({
      showOption: false
    });
  }

  _renderPickerView() {
    if (this.state.pickerData.isShow) {
      return (
        <CommonPickerView
          style={{}}
          pickerData={this.state.pickerData.data}
          pickerData2={this.state.pickerData.data2}
          type={this.state.pickerData.type}
          onClose={this._onClosePickerView.bind(this)}
          extends1={this.state.pickerData.extends1}
          extends2={this.state.pickerData.extends2}
        />
      );
    }
  }

  _onClosePickerView(selectedValue, selectedValue1) {
    this.setState({
      pickerData: { isShow: false }
    });
    if (selectedValue != undefined) {
      this.state.pickerData.callback(selectedValue, selectedValue1);
    }
  }
}
